﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Clipping.Utils;
using System.Collections.ObjectModel;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;

namespace ClippingMaster
{
    public class SearchEnginesViewModel:GalaSoft.MvvmLight.ViewModelBase
    {
        public SearchEnginesViewModel()
        {
            this._selectedSearchEngine = new ObservableCollection<ISearchEngine>();
            var allSearchEngines = Clipping.Utils.SearchEngines.SearchEngineFactory.GetAllAvailableSearchEngines();
            SettingRepository rep = new SettingRepository();
            var userSettings=rep.GetUserSettings();
            
            if (allSearchEngines != null)
            {
                List<SearchEngineModel> searchEngineModels = new List<SearchEngineModel>();
                foreach (var se in allSearchEngines)
                {
                    SearchEngineModel sem = new SearchEngineModel(se);
                    if (userSettings.SelectedSearchEngines != null && userSettings.SelectedSearchEngines.Contains(se.Name))
                    {
                        sem.IsSelected = true;
                        this.SelectedSearchEngine.Add(se);
                    }
                    sem.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(sem_PropertyChanged);
                    searchEngineModels.Add(sem);
                }
                this.AllSearchEngines = searchEngineModels;
                if (SelectedSearchEngine.Count == AllSearchEngines.Count)
                {
                    this._selectAll = true;
                    RaisePropertyChanged(() => this.SelectAll);
                }
            }
            else
                this.AllSearchEngines = new List<SearchEngineModel>();
            this.SelectAllCommand = new RelayCommand(selectAll);
            this.SaveSelectedSearchEnginesCommand = new RelayCommand(saveSelectedSearchEngines);
        }

        void sem_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            SearchEngineModel sem = sender as SearchEngineModel;
            if (e.PropertyName == "IsSelected")
            {
                if (sem.IsSelected)
                {
                    if (!SelectedSearchEngine.Contains(sem.SearchEngine))
                        SelectedSearchEngine.Add(sem.SearchEngine);

                    if (SelectedSearchEngine.Count == AllSearchEngines.Count)
                    {
                        this._selectAll = true;
                        RaisePropertyChanged(() => this.SelectAll);
                    }
                }
                else 
                {
                    if (SelectedSearchEngine.Contains(sem.SearchEngine))
                        SelectedSearchEngine.Remove(sem.SearchEngine);
                    if (SelectedSearchEngine.Count != AllSearchEngines.Count)
                    {
                        this._selectAll = false;
                        RaisePropertyChanged(() => this.SelectAll);
                    }
                }
            }
        }
        private List<SearchEngineModel> _allSearchEngines;
        public List<SearchEngineModel> AllSearchEngines
        {
            get { return _allSearchEngines; }
            set { _allSearchEngines = value; }
        }


        private ObservableCollection<ISearchEngine> _selectedSearchEngine;
        public ObservableCollection<ISearchEngine> SelectedSearchEngine
        {
            get { return _selectedSearchEngine; }
            set { _selectedSearchEngine = value; }
        }

        private bool _selectAll;
        public bool SelectAll
        {
            get { return _selectAll; }
            set 
            {
                if (value == _selectAll)
                    return;
                _selectAll = value;
               
                RaisePropertyChanged(() => this.SelectAll);
                if (_selectAll)
                {
                    selectAll();
                }
                else
                {
                    unselectAll();
                }
            }
        }


        public ICommand SelectAllCommand { get; private set; }

        public ICommand SaveSelectedSearchEnginesCommand { get; private set; }

        private void selectAll()
        {
            foreach (var sem in this.AllSearchEngines)
                sem.IsSelected = true;
        }
        private void unselectAll()
        {
            foreach (var sem in this.AllSearchEngines)
                sem.IsSelected = false;
        }

        private void saveSelectedSearchEngines()
        {
           var seList= this.SelectedSearchEngine.Select(e => e.Name).ToList();
           string str = string.Join(",", seList);
           var allSearchEngines = Clipping.Utils.SearchEngines.SearchEngineFactory.GetAllAvailableSearchEngines();
           SettingRepository rep = new SettingRepository();
           var userSettings = rep.GetUserSettings();
           userSettings.SelectedSearchEngines = str;
           rep.SaveUserSettings(userSettings);
        }
    }
}
